import React,{useEffect} from 'react'
import {Navigate,useNavigate} from 'react-router-dom'
import api from '../api'
import {message} from 'antd'
export default function Auth({children}) {
  //从localStorage获取token
  //如何将结果转成boolean类型
  const nav=useNavigate()
  const isAuth=!!localStorage.getItem('token')
  useEffect(()=>{
    getUserInfo()
  },[])
  const getUserInfo=async()=>{
     try {
        await api.users.getUserInfo()
     } catch (error) {
        message.warning('您的token已失效，请重新登录')
        nav('/login')
     }
  }
  if(isAuth){
    return (
        <>
        {children}
        </>
      )
  }else{
    message.warning('您还没有登录请登录')
    return (<>
        <Navigate to={"/login"}></Navigate>
    </>)
  }
}
